package com.example.dynamicallyadd;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.GridView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends Activity {
    private GridView gv_gridview;
    private List<EQ_Fault> mUsers;
    private Gridview_Adapter gridviewAdapter;
    private Gridview_Adapter.OnGridviewListener mOnGridviewListener = new Gridview_Adapter.OnGridviewListener() {
        @Override//添加事件
        public void onAddMembers() {
            Toast.makeText(MainActivity.this, "加号事件监听！", Toast.LENGTH_SHORT).show();
        }

        @Override//删除事件
        public void onDeleteMember(EQ_Fault user) {

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();

        initData();
        //触碰事件
        initListener();
    }

    @SuppressLint("ClickableViewAccessibility")
    private void initListener() {
        gv_gridview.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                switch (motionEvent.getAction()){
                    case MotionEvent.ACTION_DOWN:
                        //判断当前是否是删除模式，如果是删除模式
                        if (gridviewAdapter.ismIsDeleteModel()){
                            //切换为非删除模式
                            gridviewAdapter.setmIsDeleteModel(false);

                            //刷新页面
                            gridviewAdapter.notifyDataSetChanged();
                        }
                        break;
                }
                return false;
            }
        });
    }


    private void initData() {
        //初始化gridview
        initGridview();

        getData();
    }

    private void getData() {
        mUsers = new ArrayList<>();
        EQ_Fault eq_fault1 = new EQ_Fault("1","111","a","b","c","d",true);
        EQ_Fault eq_fault2 = new EQ_Fault("2","222","a","b","c","d",true);
        EQ_Fault eq_fault3 = new EQ_Fault("3","333","a","b","c","d",true);
        EQ_Fault eq_fault4 = new EQ_Fault("4","444","a","b","c","d",true);
        EQ_Fault eq_fault5 = new EQ_Fault("5","555","a","b","c","d",true);
        mUsers.add(eq_fault1);
        mUsers.add(eq_fault2);
        mUsers.add(eq_fault3);
        mUsers.add(eq_fault4);
        mUsers.add(eq_fault5);
        //刷新适配器
        gridviewAdapter.refresh(mUsers);
    }

    private void initGridview() {
        //设置操作模式
        Boolean isOperation = true;

        gridviewAdapter = new Gridview_Adapter(this,isOperation,mOnGridviewListener);

        gv_gridview.setAdapter(gridviewAdapter);


    }

    private void initView() {
        gv_gridview = findViewById(R.id.gv_groupdetail);
    }
}
